package com.zlq.cn.controller.sign.exam;


import com.zlq.cn.annotations.UnPrintResult;
import com.zlq.cn.api.SignApiResult;
import com.zlq.cn.controller.BaseController;
import com.zlq.cn.dto.param.GenTestPaper;
import com.zlq.cn.entity.ExamChapters;
import com.zlq.cn.entity.ExamQuestions;
import com.zlq.cn.service.exam.ExamChaptersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
 * 考试题库-分类
 *
 * @author zhoucl
 * @since 2024-04-10
 */
@Api(tags = "考试题库-分类")
@RestController
@RequestMapping("/exam/chapters")
public class ExamChaptersController extends BaseController {

    @Resource
    private ExamChaptersService examChaptersService;

    @ApiOperation("获取最新版本")
    @ApiImplicitParam(name = "chapterId", value = "可指定分类查询最新版本号，如果不指定就查询所有分类里面最大的版本号", paramType = "body")
    @PostMapping("/last/version")
    @UnPrintResult
    public SignApiResult<String> lastVersion(@ApiIgnore @RequestBody ExamChapters examChapters) {
        return SignApiResult.resultSuccess(examChaptersService.lambdaQuery()
                .select(ExamChapters::getVersion)
                .eq(Objects.nonNull(examChapters.getChapterId()), ExamChapters::getChapterId, examChapters.getChapterId())
                .orderByDesc(ExamChapters::getVersion)
                .last(" limit 1")
                .oneOpt().map(ExamChapters::getVersion).orElse("1"));
    }

    @UnPrintResult
    @ApiOperation("题库分类列表")
    @PostMapping("/list")
    public SignApiResult<List<ExamChapters>> list(@RequestBody ExamChapters examChapters) {
        return SignApiResult.resultSuccess(examChaptersService.list());
    }

    @UnPrintResult
    @ApiOperation("生成试卷")
    @PostMapping("/gen/test/paper")
    public SignApiResult<Collection<List<ExamQuestions>>> genTestPaper(@RequestBody GenTestPaper genTestPaper) {
        return SignApiResult.resultSuccess(examChaptersService.genTestPaper(genTestPaper));
    }
}

